package com.qfmy.webapp.service.impl.wallpaper;

import com.qfmy.common.Thread.ThreadPoolConfig;
import com.qfmy.common.exception.BaseException;
import com.qfmy.common.login.LoginUser;
import com.qfmy.common.login.LoginUserHolder;
import com.qfmy.common.result.ResultCodeEnum;
import com.qfmy.model.entity.wallpaper.Wallpaper;
import com.qfmy.model.entity.wallpaper.WallpaperRating;
import com.qfmy.webapp.mapper.wallpaper.WallpaperMapper;
import com.qfmy.webapp.mapper.wallpaper.WallpaperRatingMapper;
import com.qfmy.webapp.service.wallpaper.WallpaperRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.CompletableFuture;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.wallpaper
 * @date 2025/11/26
 * @description 壁纸评分
 */
@Service
@SuppressWarnings("all")
public class WallpaperRatingServiceImpl implements WallpaperRatingService {

    /**
     * 注入mapper
     */
    @Autowired
    private WallpaperRatingMapper wallpaperRatingMapper;

    /**
     * 注入壁纸mapper
     */
    @Autowired
    private WallpaperMapper wallpaperMapper;

    /**
     * 添加壁纸评分
     * @param id
     * @param score
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addWallpaperRating(Integer id, Double score) {
       //获取当前用户
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断
        if(loginUser == null)
        {
            throw  new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        //获取用户id
        Long userId = loginUser.getUserId();
        //创建壁纸评分对象
        WallpaperRating wallpaperRating = new WallpaperRating();
        wallpaperRating.setUserId(userId);
        wallpaperRating.setWallpaperId(id);
        wallpaperRating.setScore(score);
        wallpaperRatingMapper.addWallpaperRating(wallpaperRating);
        //异步更新壁纸的评分
        CompletableFuture.runAsync(() ->{
            //根据壁纸id查询壁纸
            Wallpaper wallpaper = wallpaperMapper.getWallpaperById(id);
            //获取壁纸评分
            double rate = wallpaper.getScore();
            //获取壁纸评分人数
            Integer count = wallpaper.getScoreCount();
            //重新计算壁纸评分 = (壁纸评分 * 壁纸评分人数 + 新评分) / (壁纸评分人数 + 1)
            double newRate = (rate * count + score) / (count + 1);
            wallpaperMapper.updateScore(id, newRate, count + 1);
        }, ThreadPoolConfig.executorService);

    }

    /**
     * 判断用户是否已经评分
     * @param id
     * @return
     */
    @Override
    public double isRated(Integer id) {
        //获取当前用户
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断
        if(loginUser == null)
        {
            throw  new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        Long userId = loginUser.getUserId();
        //根据用户id和壁纸id查询
        WallpaperRating wallpaperRating = wallpaperRatingMapper.selectWallpaperRating(userId, id);
        if(wallpaperRating == null)
        {
            return 0;
        }
        return wallpaperRating.getScore();
    }
}
